﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;

namespace MainService
{
    public class BusinessEntities
    {

    }
    public class UserOperations
    {
        private UserDetails GetUserDetail(string userName, string password)
        {
            UserDetails usDetails = new UserDetails();
            DataSet ds = new DataSet();
            DAO d = new DAO();
            List<SqlParameter> param = new List<SqlParameter>();
            param.Add(d.AddParameter("@UserName", userName));
            param.Add(d.AddParameter("@Password", password));
            ds = d.ExecuteDataSet("P_ValidateUser", param);
            if (ds.Tables.Count > 0)
            {
                if (ds.Tables[0] != null && ds.Tables[0].Rows.Count > 0)
                {
                    usDetails.UserName = userName;
                    usDetails.UserId = Convert.ToInt32(ds.Tables[0].Rows[0]["UserId"]);
                    usDetails.ClientId = Convert.ToInt32(ds.Tables[0].Rows[0]["ClientId"]);
                    usDetails.DeviceLimit = Convert.ToInt32(ds.Tables[0].Rows[0]["DeviceLimit"]);
                    usDetails.Company = Convert.ToString(ds.Tables[0].Rows[0]["Company"]);
                    usDetails.EmailId = Convert.ToString(ds.Tables[0].Rows[0]["EmailAddress"]);
                }
                if (ds.Tables[1] != null && ds.Tables[1].Rows.Count > 0)
                {
                    usDetails.HashCode = Convert.ToString(ds.Tables[1].Rows[0]["HashCode"]);
                    usDetails.LastLogin = Convert.ToString(ds.Tables[1].Rows[0]["LoggedInTime"]);
                }
                if (ds.Tables[2] != null && ds.Tables[2].Rows.Count > 0)
                {
                    Dictionary<int, string> lstService = new Dictionary<int, string>();
                    for (int i = 0; i < ds.Tables[2].Rows.Count; i++)
                    {
                        lstService.Add(Convert.ToInt32(ds.Tables[2].Rows[i]["ItemId"]), Convert.ToString(ds.Tables[2].Rows[i]["ServiceName"]));
                    }
                    usDetails.GetServiceList = lstService;
                }
                usDetails.IsUserValidated = true;
                usDetails.LoginMessage = "User Validated Successfully";

            }
            else
            {
                usDetails.LoginMessage = "Invalid Login Details";
            }
            return usDetails;
        }

        internal string AddUser(string emailId, string appUserName, string password, string company)
        {
            /* object messgae = null;
             DAO d = new DAO();
              List<SqlParameter> param = new List<SqlParameter>();
              param.Add(d.AddParameter("@EmailId", emailId));
              param.Add(d.AddParameter("@UserName", appUserName));
              param.Add(d.AddParameter("@Password", password));
              param.Add(d.AddParameter("@Company", company));
              param.Add(d.AddParameter("@Messages", messgae,SqlDbType.VarChar,500,ParameterDirection.InputOutput));
              d.ExecuteQuery("P_AddUser", param, ref messgae);
         
              return messgae.ToString();
             *   * */
            return "Method not implemented";
        }

        internal UserDetails ValidateUser(string UserName, string _password)
        {
            return GetUserDetail(UserName, _password);
        }

        internal string AddUser(int userId,string EmailId, string UserName, string _password, string Company, int DeviceLimit, int ClientId, List<string> ServiceList,string hashcode)
        {
            object messgae = null;
            DAO d = new DAO();

            List<SqlParameter> param = new List<SqlParameter>();
            param.Add(d.AddParameter("@UserId", userId));
            param.Add(d.AddParameter("@HashCode", hashcode));
            param.Add(d.AddParameter("@EmailId", EmailId));
            param.Add(d.AddParameter("@UserName", UserName));
            param.Add(d.AddParameter("@Password", _password));
            param.Add(d.AddParameter("@Company", Company));
            param.Add(d.AddParameter("@DeviceCount", DeviceLimit));
            if (ServiceList != null && ServiceList.Count > 0)
            {
                string serviceListcsv = string.Join(",", ServiceList.Select(x => x).ToArray());
                param.Add(d.AddParameter("@ServiceList", serviceListcsv));
            }
            if (ClientId > 0)
            {
                param.Add(d.AddParameter("@ClientId", ClientId));
            }
            param.Add(d.AddParameter("@Messages", messgae, SqlDbType.VarChar, 500, ParameterDirection.InputOutput));
            d.ExecuteQuery("P_AddUser", param, ref messgae);
            return messgae.ToString();
        }

        internal string DeleteUser(int UserId, int ClientId,string hashCode)
        {
            object messgae = null;
            DAO d = new DAO();

            List<SqlParameter> param = new List<SqlParameter>();
            param.Add(d.AddParameter("@UserId", UserId));
             param.Add(d.AddParameter("@ClientId", ClientId));
             param.Add(d.AddParameter("@HashCode", hashCode));
            param.Add(d.AddParameter("@Messages", messgae, SqlDbType.VarChar, 500, ParameterDirection.InputOutput));
            d.ExecuteQuery("P_DeleteUser", param, ref messgae);
            return messgae.ToString();
        }

        internal string UpdateUser(int UserId,string UserName, string _password, int DeviceLimit, int ClientId, List<string> ServiceList, string HashCode)
        {
            object messgae = null;
            DAO d = new DAO();

            List<SqlParameter> param = new List<SqlParameter>();
            param.Add(d.AddParameter("@UserId", UserId));
            param.Add(d.AddParameter("@UserName", UserName));
            param.Add(d.AddParameter("@Password", _password));
            param.Add(d.AddParameter("@HashCode", HashCode));
            param.Add(d.AddParameter("@DeviceCount", DeviceLimit));
            if (ServiceList != null && ServiceList.Count > 0)
            {
                string serviceListcsv = string.Join(",", ServiceList.Select(x => x).ToArray());
                param.Add(d.AddParameter("@ServiceList", serviceListcsv));
            }
            if (ClientId > 0)
            {
                param.Add(d.AddParameter("@ClientId", ClientId));
            }
            param.Add(d.AddParameter("@Messages", messgae, SqlDbType.VarChar, 500, ParameterDirection.InputOutput));
            d.ExecuteQuery("P_UpdateUser", param, ref messgae);
            return messgae.ToString();
        }

        internal List<UserDetails> ListUsers(int clientId, string HashCode)
        {
            List<UserDetails> lstUsers = new List<UserDetails>();
            UserDetails usDetails = null;
            DataSet ds = new DataSet();
            DAO d = new DAO();
            int itemId = 0;
            List<SqlParameter> param = new List<SqlParameter>();
            param.Add(d.AddParameter("@ClientId", clientId));
            param.Add(d.AddParameter("@HashCode", HashCode));
            ds = d.ExecuteDataSet("P_ListUsers", param);
            if (ds.Tables.Count > 0)
            {
                if (ds.Tables[0] != null && ds.Tables[0].Rows.Count > 0)
                {
                    for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                    {
                        usDetails = new UserDetails();
                        usDetails.UserName = Convert.ToString(ds.Tables[0].Rows[i]["UserName"]);
                        usDetails.UserId = Convert.ToInt32(ds.Tables[0].Rows[i]["UserId"]);
                        usDetails.ClientId = Convert.ToInt32(ds.Tables[0].Rows[i]["ClientId"]);
                        usDetails.DeviceLimit = Convert.ToInt32(ds.Tables[0].Rows[i]["DeviceLimit"]);
                        usDetails.LastLogin = Convert.ToString(ds.Tables[0].Rows[i]["LoggedInTime"]);
                        usDetails.CreateDate = Convert.ToString(ds.Tables[0].Rows[i]["CreateDate"]);
                        string[] itemIds = Convert.ToString(ds.Tables[0].Rows[i]["ItemIds"]).Split(',');
                        string[] serviceNames = Convert.ToString(ds.Tables[0].Rows[i]["ServiceName"]).Split(',');
                        Dictionary<int, string> lstService = new Dictionary<int, string>();
                        for (int j = 0; j < itemIds.Length; j++)
                        {
                            if (Int32.TryParse(itemIds[j],out itemId) && itemId>0)
                            {
                                lstService.Add(itemId, Convert.ToString(serviceNames[j]));
                            }
                        }
                        //for (int j = 0; j < ds.Tables[0].Rows.Count; j++)
                        //{
                        //    lstService.Add(Convert.ToInt32(ds.Tables[2].Rows[i]["ItemId"]), Convert.ToString(ds.Tables[2].Rows[i]["ServiceName"]));
                        //}
                        usDetails.GetServiceList = lstService;
                        lstUsers.Add(usDetails);
                    }
                }
                else
                {
                    usDetails = new UserDetails();
                    usDetails.LoginMessage = "No Data Found";
                    lstUsers.Add(usDetails);
                }
            }
            else
            {
                usDetails = new UserDetails();
                usDetails.LoginMessage = "No Row Found";
                lstUsers.Add(usDetails);
            }

           
            return lstUsers;
        }

        internal UserDetails ListUser(int userId, int clientId, string HashCode)
        {
            UserDetails usDetails = new UserDetails();
            DataSet ds = new DataSet();
            DAO d = new DAO();
            int itemId = 0;
            List<SqlParameter> param = new List<SqlParameter>();
            param.Add(d.AddParameter("@UserId", userId));
            param.Add(d.AddParameter("@ClientId", clientId));
            param.Add(d.AddParameter("@HashCode", HashCode));
            ds = d.ExecuteDataSet("P_GetAppUser", param);
            if (ds.Tables.Count > 0)
            {
                if (ds.Tables[0] != null && ds.Tables[0].Rows.Count > 0)
                {
                        usDetails.UserName = Convert.ToString(ds.Tables[0].Rows[0]["UserName"]);
                        usDetails.Password = Convert.ToString(ds.Tables[0].Rows[0]["Password"]);
                        usDetails.UserId = Convert.ToInt32(ds.Tables[0].Rows[0]["UserId"]);
                        usDetails.ClientId = Convert.ToInt32(ds.Tables[0].Rows[0]["ClientId"]);
                        usDetails.DeviceLimit = Convert.ToInt32(ds.Tables[0].Rows[0]["DeviceLimit"]);
                        usDetails.CreateDate = Convert.ToString(ds.Tables[0].Rows[0]["CreateDate"]);
                        string[] itemIds = Convert.ToString(ds.Tables[0].Rows[0]["ItemIds"]).Split(',');
                       // string[] serviceNames = Convert.ToString(ds.Tables[0].Rows[0]["ServiceName"]).Split(',');
                        Dictionary<int, string> lstService = new Dictionary<int, string>();
                        for (int j = 0; j < itemIds.Length; j++)
                        {
                            if (Int32.TryParse(itemIds[j], out itemId) && itemId > 0)
                            {
                                lstService.Add(itemId, "");
                            }
                        }                       
                        usDetails.GetServiceList = lstService;
                        usDetails.LoginMessage = "0";
                }
                else
                {
                    usDetails.LoginMessage = "No Data Found";
                }
            }
            else
            {
                usDetails.LoginMessage = "No Row Found";
            }


            return usDetails;
        }
    }

    public class DatabaseOperations
    {

        internal string CreateNewDatabase(string DBName, bool IsCaseSensitiveValidation, int CreatedBy, List<ScanData> ScanDataList, List<int> ServiceDetails, ref int newDBId)
        {
            object messgae = null;
            object dbIDOutput = null;
            int dbId = 0;
            DAO d = new DAO();
          
            List<SqlParameter> param = new List<SqlParameter>();
            param.Add(d.AddParameter("@DBName", DBName));
            param.Add(d.AddParameter("@IsCaseSensitive", IsCaseSensitiveValidation));
            param.Add(d.AddParameter("@CreatedBy", CreatedBy));
            if (ServiceDetails != null && ServiceDetails.Count > 0)
            {
                string serviceListcsv = string.Join(",", ServiceDetails.Select(x => x.ToString()).ToArray());
                param.Add(d.AddParameter("@ServiceList", serviceListcsv));
            }
            else
            {
                param.Add(d.AddParameter("@ServiceList", ""));
            }
            param.Add(d.AddParameter("@DBId", dbIDOutput, SqlDbType.Int, 10, ParameterDirection.InputOutput));
            param.Add(d.AddParameter("@Messages", messgae, SqlDbType.VarChar, 500, ParameterDirection.InputOutput));
            d.ExecuteQuery("P_AddDataBase", param, ref dbIDOutput, ref messgae);
            if (dbIDOutput != null && Int32.TryParse(dbIDOutput.ToString(), out dbId))
            {
                newDBId = dbId;
                if (ScanDataList != null)
                {
                    foreach (ScanData item in ScanDataList)
                    {
                        AddScanData(CreatedBy, dbId, item);
                    }
                }
            }
            return messgae.ToString();
        }

        internal string AddScanData(int CreatedBy, int dbId, ScanData item)
        {
            object msg = null;
            DAO d = new DAO();
            List<SqlParameter> param = new List<SqlParameter>();
            param.Add(d.AddParameter("@DBId", dbId));
            param.Add(d.AddParameter("@BarCodeValue", item.BarcodeVale));
            param.Add(d.AddParameter("@BarCodeResponse", item.BarcodeResponse));
            param.Add(d.AddParameter("@Validity", item.Validity));
            param.Add(d.AddParameter("@CreatedBy", CreatedBy));
            param.Add(d.AddParameter("@Messages", msg, SqlDbType.VarChar, 500, ParameterDirection.InputOutput));
            d.ExecuteQuery("P_AddScanData", param, ref msg);
            return msg.ToString();
        }

        internal string RenameDatabase(int createdBy, int dbId, string newName)
        {
            object msg = null;
            DAO d = new DAO();
            List<SqlParameter> param = new List<SqlParameter>();
            param.Add(d.AddParameter("@DBId", dbId));
            param.Add(d.AddParameter("@NewName", newName));
            param.Add(d.AddParameter("@CreatedBy", createdBy));
            param.Add(d.AddParameter("@Messages", msg, SqlDbType.VarChar, 500, ParameterDirection.InputOutput));
            d.ExecuteQuery("P_RenameDB", param, ref msg);
            return msg.ToString();
        }

        internal string DeleteDatabase(int CreatedBy, int DbId)
        {
            object msg = null;
            DAO d = new DAO();
            List<SqlParameter> param = new List<SqlParameter>();
            param.Add(d.AddParameter("@DBId", DbId));
            param.Add(d.AddParameter("@CreatedBy", CreatedBy));
            param.Add(d.AddParameter("@Messages", msg, SqlDbType.VarChar, 500, ParameterDirection.InputOutput));
            d.ExecuteQuery("P_DeleteDB", param, ref msg);
            return msg.ToString();
        }

        internal DatabaseDetails GetDatabaseDetails(int CreatedBy, int DbId)
        {
            return GetDBDetail(DbId, CreatedBy);
        }

        private DatabaseDetails GetDBDetail(int DbId, int CreatedBy)
        {
            DatabaseDetails dbDetails = new DatabaseDetails();
            DataSet ds = new DataSet();
            DAO d = new DAO();
            List<SqlParameter> param = new List<SqlParameter>();
            param.Add(d.AddParameter("@DBId", DbId));
            param.Add(d.AddParameter("@CreatedBy", CreatedBy));
            ds = d.ExecuteDataSet("P_GetDBDetail", param);
            if (ds.Tables.Count > 0)
            {
                if (ds.Tables[0] != null && ds.Tables[0].Rows.Count > 0)
                {
                    dbDetails.DbId = DbId;
                    dbDetails.DBName = Convert.ToString(ds.Tables[0].Rows[0]["DBName"]);
                    dbDetails.IsCaseSensitiveValidation = Convert.ToBoolean(ds.Tables[0].Rows[0]["CaseSensitive"]);
                    dbDetails.LastUpdated = Convert.ToDateTime(ds.Tables[0].Rows[0]["UpdateDate"]);
                }
                if (ds.Tables[1] != null && ds.Tables[1].Rows.Count > 0)
                {
                    dbDetails.RowCount = Convert.ToInt32(ds.Tables[1].Rows[0]["Count"]);
                }
                if (ds.Tables[2] != null && ds.Tables[2].Rows.Count > 0)
                {
                    Dictionary<int, string> lstService = new Dictionary<int, string>();
                    for (int i = 0; i < ds.Tables[2].Rows.Count; i++)
                    {
                        lstService.Add(Convert.ToInt32(ds.Tables[2].Rows[i]["ServiceId"]), Convert.ToString(ds.Tables[2].Rows[i]["ServiceName"]));
                    }
                    dbDetails.GetServiceDetails = lstService;
                }

            }
            else
            {
                dbDetails.UserMessage = "Unbale to get db details";
            }
            return dbDetails;
        }

        internal string AssociateServicToDatabase(int DbId, List<int> ServiceDetails, int CreatedBy)
        {
            object messgae = null;
            DAO d = new DAO();
            string serviceListcsv = string.Join(",", ServiceDetails.Select(x => x.ToString()).ToArray());
            List<SqlParameter> param = new List<SqlParameter>();
            param.Add(d.AddParameter("@DBId", DbId));
            param.Add(d.AddParameter("@CreatedBy", CreatedBy));
            param.Add(d.AddParameter("@ServiceList", serviceListcsv));
            param.Add(d.AddParameter("@Messages", messgae, SqlDbType.VarChar, 500, ParameterDirection.InputOutput));
            d.ExecuteQuery("P_AssociateService", param, ref messgae);

            return messgae.ToString();
        }

        internal List<int> GetDBIDList(int clientId)
        {
            List<int> lstDBIds = new List<int>();
            DataSet ds = new DataSet();
            DAO d = new DAO();
            List<SqlParameter> param = new List<SqlParameter>();
            param.Add(d.AddParameter("@ClientId", clientId));
            ds = d.ExecuteDataSet("P_GetDBList", param);
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    lstDBIds.Add(Convert.ToInt32(ds.Tables[0].Rows[i]["DataBaseId"]));
                }

            }
            return lstDBIds;
        }

        internal DataTable GetDBIDList(int DbId, int CreatedBy)
        {
            DataSet ds = new DataSet();
            DAO d = new DAO();
            List<SqlParameter> param = new List<SqlParameter>();
            param.Add(d.AddParameter("@DBId", DbId));
            param.Add(d.AddParameter("@CreatedBy", CreatedBy));
            ds = d.ExecuteDataSet("P_GetTableData", param);
            return ds.Tables[0];
        }
    }

    public class ServiceOperations
    {

        internal Dictionary<int, string> GetServices(int clientId, string hashCode)
        {
            Dictionary<int, string> _dcData = new Dictionary<int, string>();
            List<int> lstDBIds = new List<int>();
            DataSet ds = new DataSet();
            DAO d = new DAO();
            List<SqlParameter> param = new List<SqlParameter>();
            param.Add(d.AddParameter("@ClientId", clientId));
            param.Add(d.AddParameter("@HashCode", hashCode));
            ds = d.ExecuteDataSet("P_GetServiceList", param);
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    _dcData.Add(Convert.ToInt32(ds.Tables[0].Rows[i]["ServiceId"]), Convert.ToString(ds.Tables[0].Rows[i]["ServiceName"]));
                }

            }
            return _dcData;
        }
    }
}